/**
 * 
 */
package com.EstateManager.Dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;





import com.EstateManager.Entity.Estatebasicinfo;
import com.EstateManager.Entity.User;

/**
 * 创建时间 : 上午10:49:58
 * @author Yonggang Liu
 *
 * EstateBasicInfoDao.java
 */
@Repository("estateBasicInfoDao")
public class EstateBasicInfoDao implements IModelBasicOperate<Estatebasicinfo, Integer> {
	
	/*房产信息审核状态*/
	public final static String CHECKTYPE_PASS = "已通过";
	
	public final static String CHECKTYPE_NOPASS = "未通过";
	
	public final static String CHECKTYPE_NOCHECK = "待审核";
	
	public final static String PUBCLICSTATUE_NOPUBLIC = "待发布";
	
	public final static String PUBCLICSTATUE_PUBLISHED = "已发布";
	
	public final static String PUBCLICSTATUE_FINISH_PUBLISHE = "发布完成";
	
	
	
	
	
	@Autowired
	private SessionFactory sessionFactory;
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	
	@Override
	public Estatebasicinfo load(Integer id) throws HibernateException {
		return (Estatebasicinfo)this.getCurrentSession().load(Estatebasicinfo.class, id);
	}
	
	
	@Override
	public Estatebasicinfo get(Integer id) throws HibernateException {
		return (Estatebasicinfo)this.getCurrentSession().get(Estatebasicinfo.class, id);
	}
	
	
	@Override
	public void persist(Estatebasicinfo entity) throws HibernateException {
		this.getCurrentSession().persist(entity);
		
	}
	
	
	@Override
	public Integer save(Estatebasicinfo entity) throws HibernateException {
		return (Integer)this.getCurrentSession().save(entity);
	}
	
	
	@Override
	public void saveOrUpdate(Estatebasicinfo entity) throws HibernateException {
		this.getCurrentSession().saveOrUpdate(entity);
		

	}
	
	@Override
	public void detete(Estatebasicinfo entity) throws HibernateException {
		this.getCurrentSession().delete(entity);
	}
	
	@Override
	public void flush() throws HibernateException {
		
		this.getCurrentSession().flush();
		
	}
	
	@Override
	public void update(Estatebasicinfo t) throws HibernateException {

		this.getCurrentSession().update(t);
		
	}
	
	
	@Override
	public void merge(Estatebasicinfo t) throws HibernateException {
		
		this.getCurrentSession().merge(t);
		
	}
	
	/**
	 * 根据审核状态查询房产数据信息页
	 * @param checkState
	 * @param pageSize 
	 * @param pageNum
	 * */
	public List<Estatebasicinfo> getListByEstateCheckState(String checkState, int pageSize, int pageNum) {
		Session session = this.getCurrentSession();
		Query query = session.createQuery("select estatebasicinfo "
										+ "from Estatebasicinfo estatebasicinfo " 
										+ "where estatebasicinfo.estateInfoCheckState = :checkState");
		query.setParameter("checkState", checkState);
		query.setMaxResults(pageSize);
		query.setFirstResult(pageSize * (pageNum-1));
		List<Estatebasicinfo> list = (List<Estatebasicinfo>)query.list();
		
		return list;
	}
	
	/**
	 * 根据用户Id查询房产数据信息页
	 * @param userId
	 * @param pageSize 
	 * @param pageNum
	 * */
	public List<Estatebasicinfo> getListByUserid(int pageSize, int pageNum, int userId){
		User user = new User();
		user.setId(userId);
		Query query = this.getCurrentSession().createQuery(""
				+ "select estatebasicinfo "
				+ "from Estatebasicinfo estatebasicinfo "
				+ "where estatebasicinfo.user = :user");
		query.setParameter("user", user);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum-1) * pageSize);
		return (List<Estatebasicinfo>)query.list();
	}
	
	/**
	 * 根据审核状态和用户ID查询房产数据信息页
	 * @param estateInfoCheckState
	 * @param pageSize 
	 * @param pageNum
	 * @param id
	 * */
	public List<Estatebasicinfo>  getListByUserAndChecktype(int pageSize, int pageNum, int id, String estateInfoCheckState) {
		User user = new User();
		user.setId(id);
		Query query = this.getCurrentSession().createQuery(""
				+ "select estatebasicinfo "
				+ "from Estatebasicinfo estatebasicinfo "
				+ "where estatebasicinfo.user = :user and estatebasicinfo.estateInfoCheckState = :estateInfoCheckState");
		query.setParameter("user", user);
		query.setParameter("estateInfoCheckState", estateInfoCheckState);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum-1) * pageSize);
		return (List<Estatebasicinfo>)query.list();
				
	}
	
	
	/**
	 * 根据审核状态和用户ID和发布信息状态查询房产数据信息页
	 * @param estateInfoCheckState
	 * @param pageSize 
	 * @param pageNum
	 * @param id
	 * @param publicStatu
	 * */
	public List<Estatebasicinfo>  getEstateListByUserCheckstatePublicstatu(int pageSize, int pageNum, int id, String estateInfoCheckState, String publicStatu) {
		User user = new User();
		user.setId(id);
		Query query = this.getCurrentSession().createQuery(""
				+ "select estatebasicinfo "
				+ "from Estatebasicinfo estatebasicinfo "
				+ "where estatebasicinfo.user = :user and estatebasicinfo.estateInfoCheckState = :estateInfoCheckState and estatebasicinfo.publicStatu = :publicStatu");
		query.setParameter("user", user);
		query.setParameter("estateInfoCheckState", estateInfoCheckState);
		query.setParameter("publicStatu", publicStatu);
		query.setMaxResults(pageSize);
		query.setFirstResult((pageNum-1) * pageSize);
		return (List<Estatebasicinfo>)query.list();
	
	
	}
}
